﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using GameFrameLibrary;
using System.Windows;
using System.Windows.Controls;

namespace Prism.Games.GalEngine.Scripts.Parser
{
    [Serializable]
    public class GameConfig
    {
        public GameConfig(WindowConfig config)
        {
            this.WindowConfig = config;

            this.FullScreenBB = (Helper.GraphicBase.GalUIFrame.ConfigFrame as FrameworkElement).FindName(GameFrameLibrary.Strings.FullScreenBB) as BoolenButton;
            this.WindowBB = (Helper.GraphicBase.GalUIFrame.ConfigFrame as FrameworkElement).FindName(GameFrameLibrary.Strings.WindowBB) as BoolenButton;
            this.SmallSizeBB = (Helper.GraphicBase.GalUIFrame.ConfigFrame as FrameworkElement).FindName(GameFrameLibrary.Strings.SmallSizeBB) as BoolenButton;
            this.OriginalSizeBB = (Helper.GraphicBase.GalUIFrame.ConfigFrame as FrameworkElement).FindName(GameFrameLibrary.Strings.OriginalSizeBB) as BoolenButton;

            this.SkipNotReadBB = (Helper.GraphicBase.GalUIFrame.ConfigFrame as FrameworkElement).FindName(GameFrameLibrary.Strings.ReadSkipBB) as BoolenButton;
            this.StopNotReadBB = (Helper.GraphicBase.GalUIFrame.ConfigFrame as FrameworkElement).FindName(GameFrameLibrary.Strings.ReadStopBB) as BoolenButton;


            this.FullScreenBB.IsChecked = this.WindowConfig.IsFullScreen;
            this.WindowBB.IsChecked = this.WindowConfig.IsWindow;
            this.OriginalSizeBB.IsChecked = this.WindowConfig.IsOriginalSize;
            this.SmallSizeBB.IsChecked = this.WindowConfig.IsSmallSize;
            this.StopNotReadBB.IsChecked = this.WindowConfig.IsStopNotRead;
            this.SkipNotReadBB.IsChecked = this.WindowConfig.IsSkipNotRead;

            this.BGMSlider = (Helper.GraphicBase.GalUIFrame.ConfigFrame as FrameworkElement).FindName(GameFrameLibrary.Strings.BGMSliderBB) as Slider;
            this.SESlider = (Helper.GraphicBase.GalUIFrame.ConfigFrame as FrameworkElement).FindName(GameFrameLibrary.Strings.SESliderBB) as Slider;
            this.VocalSlider = (Helper.GraphicBase.GalUIFrame.ConfigFrame as FrameworkElement).FindName(GameFrameLibrary.Strings.VocalSliderBB) as Slider;
            this.EffectSlider = (Helper.GraphicBase.GalUIFrame.ConfigFrame as FrameworkElement).FindName(GameFrameLibrary.Strings.EffectSliderBB) as Slider;

            this.BGMSlider.Value = Convert.ToDouble(this.WindowConfig.BGMVolume * 10);
            Helper.Parser.BgmParser.ChangeVolume(this.WindowConfig.BGMVolume);
            Helper.Parser.MenuBgmParser.ChangeVolume(this.WindowConfig.BGMVolume);


            this.SESlider.Value = Convert.ToDouble(this.WindowConfig.SEVolume * 10);
            Helper.Parser.SystemSoundParser.ChangeVolume(this.WindowConfig.SEVolume);


            this.VocalSlider.Value = Convert.ToDouble(this.WindowConfig.VocalVolume * 10);
            Helper.Parser.VocalParser.ChangeVolume(this.WindowConfig.VocalVolume);


            this.EffectSlider.Value = Convert.ToDouble(this.WindowConfig.EffectVolume * 10);
            Helper.Parser.ESParser.ChangeVolume(this.WindowConfig.EffectVolume);

            this.SkipNotReadBB.IsChecked = this.WindowConfig.IsSkipNotRead;
            this.StopNotReadBB.IsChecked = this.WindowConfig.IsStopNotRead;

        }

        public void SetAudioVolume(RoutedPropertyChangedEventArgs<double> e)
        {
            var slider = e.Source as Slider;
            var volume = Convert.ToSingle((e.NewValue / 10d));
            switch (slider.Name)
            {
                case GameFrameLibrary.Strings.BGMSliderBB:
                    Helper.Parser.BgmParser.ChangeVolume(volume);
                    Helper.Parser.MenuBgmParser.ChangeVolume(volume);
                    this.WindowConfig.BGMVolume = volume;
                    break;
                case GameFrameLibrary.Strings.SESliderBB:
                    Helper.Parser.SystemSoundParser.ChangeVolume(volume);
                    this.WindowConfig.SEVolume = volume;
                    break;
                case GameFrameLibrary.Strings.EffectSliderBB:
                    Helper.Parser.ESParser.ChangeVolume(volume);
                    this.WindowConfig.EffectVolume = volume;
                    break;
                case GameFrameLibrary.Strings.VocalSliderBB:
                    Helper.Parser.VocalParser.ChangeVolume(volume);
                    this.WindowConfig.VocalVolume = volume;
                    break;
                default:
                    break;
            }
        }

        public void SwitchNotRead(bool isSkipNotRead)
        {
            if (isSkipNotRead)
            {
                this.WindowConfig.IsSkipNotRead = true;
                this.WindowConfig.IsStopNotRead = false;
                this.SkipNotReadBB.IsChecked = true;
                this.StopNotReadBB.IsChecked = false;
            }
            else
            {
                this.WindowConfig.IsSkipNotRead = false;
                this.WindowConfig.IsStopNotRead = true;
                this.SkipNotReadBB.IsChecked = false;
                this.StopNotReadBB.IsChecked = true;
            }
        }


        public void SwitchFullScreen()
        {
            if (this.WindowConfig.IsWindow)
            {
                Helper.GraphicBase.GameWindow.FullScreen();
                this.WindowConfig.IsFullScreen = true;
                this.FullScreenBB.IsChecked = true;
                this.WindowConfig.IsWindow = false;
                this.WindowBB.IsChecked = false;
                return;
            }
        }

        public void WindowMode()
        {
            if (this.WindowConfig.IsFullScreen)
            {
                Helper.GraphicBase.GameWindow.ResumeWindowMode();
                this.WindowConfig.IsFullScreen = false;
                this.FullScreenBB.IsChecked = false;
                this.WindowConfig.IsWindow = true;
                this.WindowBB.IsChecked = true;
                return;
            }
        }

        public void OriginalSize()
        {
            if (this.WindowConfig.IsFullScreen)
                return;

            if (this.WindowConfig.IsSmallSize)
            {
                Helper.GraphicBase.GameWindow.Zoom(false);
                this.WindowConfig.IsSmallSize = false;
                this.SmallSizeBB.IsChecked = false;
                this.WindowConfig.IsOriginalSize = true;
                this.OriginalSizeBB.IsChecked = true;
                return;
            }
        }

        public void SmallSize()
        {
            if (this.WindowConfig.IsFullScreen)
                return;

            if (this.WindowConfig.IsOriginalSize)
            {
                Helper.GraphicBase.GameWindow.Zoom(true);
                this.WindowConfig.IsSmallSize = true;
                this.SmallSizeBB.IsChecked = true;
                this.WindowConfig.IsOriginalSize = false;
                this.OriginalSizeBB.IsChecked = false;
                return;
            }
        }


        public WindowConfig WindowConfig { get; set; }

        [field: NonSerialized]
        protected BoolenButton FullScreenBB;
        [field: NonSerialized]
        protected BoolenButton WindowBB;
        [field: NonSerialized]
        protected BoolenButton SmallSizeBB;
        [field: NonSerialized]
        protected BoolenButton OriginalSizeBB;

        [field: NonSerialized]
        protected BoolenButton SkipNotReadBB;
        [field: NonSerialized]
        protected BoolenButton StopNotReadBB;

        [field: NonSerialized]
        protected Slider BGMSlider;
        [field: NonSerialized]
        protected Slider SESlider;
        [field: NonSerialized]
        protected Slider VocalSlider;
        [field: NonSerialized]
        protected Slider EffectSlider;
    }
}
